// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import type { NextApiRequest, NextApiResponse } from 'next';
import { BeError, BeSuccess, ResponseDataType } from '@/utils/response';
import { syncProjects } from '@/services/gitlab';

export default async function handler(req: NextApiRequest, res: NextApiResponse<ResponseDataType>) {
  if (req.method?.toLowerCase() !== 'get') {
    res.status(200).json(BeError('不支持请求类型'));
    res.end();
    return;
  }

  const { page, pageSize } = req.query;
  try {
    const ret = {
      status: 200,
      statusText: '假数据都报错？',
    };
    // const ret = await axios.get(`${baseUrl}/projects`, {
    //   params: {
    //     private_token: privateToken,
    //     per_page: pageSize || perPage,
    //     page: page || 1,
    //   },
    // });
    if (ret.status === 200) {
      // res.status(200).json(BeSuccess(ret.data));
      // const data = require('../../../logs/gitprojects.json');
      // res.status(200).json(BeSuccess(data.data));
      const result = await syncProjects();
      res.status(200).json(BeSuccess(result));
    } else {
      res.status(ret.status).json(BeError(ret.statusText));
    }
  } catch (error: any) {
    res.status(500).json(BeError(error));
  }
}
